package com.solomon.travel.common.util;

import java.util.ResourceBundle;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * <p>
 * redis通用工具类
 * </p>
 * 
 * @author bigSea
 *
 */
public class RedisUtil {

	private static JedisPool pool;

	static {
		ResourceBundle bundle = ResourceBundle.getBundle("redis");
		if (bundle == null) {
			throw new IllegalArgumentException("[redis.properties] is not found!");
		}
		JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxTotal")));
		config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
		config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));
		config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
		config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));
		pool = new JedisPool(config, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")),
				Integer.valueOf(bundle.getString("redis.timeout")),bundle.getString("redis.auth"));
	}

	/**
	 * <p>
	 * 通过key获取储存在redis中的value
	 * </p>
	 * <p>
	 * 并释放连接
	 * </p>
	 * 
	 * @param key
	 * @return 成功返回value 失败返回null
	 */
	public static String get(String key) {
		Jedis jedis = null;
		String value = null;
		try {
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return value;
	}

	/**
	 * <p>
	 * 向redis存入key和value,并释放连接资源
	 * </p>
	 * <p>
	 * 如果key已经存在 则覆盖
	 * </p>
	 * 
	 * @param key
	 * @param value
	 * @return 成功 返回OK 失败返回 0
	 */
	public static String set(String key, String value) {
		Jedis jedis = null;
		try {
			jedis = pool.getResource();
			return jedis.set(key, value);
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static String set(byte[] objectKey, byte[] objectValue) {
		Jedis jedis = null;
		try {
			jedis = pool.getResource();
			return jedis.set(objectKey, objectValue);
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}

	}

	public static byte[] get(byte[] objectKey) {
		Jedis jedis = null;
		byte[] value = null;
		try {
			jedis = pool.getResource();
			value = jedis.get(objectKey);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return value;
	}
	
	public static Long del(byte[]... keys) {
		Jedis jedis = null;
		try {
			jedis = pool.getResource();
			return jedis.del(keys);
		} catch (Exception e) {
			e.printStackTrace();
			return 0l;
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}
	
	public static Long del(String... keys) {
		Jedis jedis = null;
		try {
			jedis = pool.getResource();
			return jedis.del(keys);
		} catch (Exception e) {
			e.printStackTrace();
			return 0l;
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static Object expire(byte[] objectKey, int seconds) {
		Jedis jedis = null;
		try {
			jedis = pool.getResource();
			return jedis.expire(objectKey, seconds);
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static void flushDB() {
		Jedis jedis = null;
		try {
			jedis = pool.getResource();
			jedis.flushDB();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}

	public static Object dbSize() {
		Jedis jedis = null;
		try {
			jedis = pool.getResource();
			return jedis.dbSize();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
	}
}